package com.ciwong.epaper.modules.me.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.common.db.DatabaseHelper;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.download.DownLoadDB;
import com.ciwong.libs.utils.FileUtils;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.FileUtil;
import com.ciwong.mobilelib.utils.ThreadTask;
import com.ciwong.mobilelib.widget.CWDialog;

import java.io.File;

/**
 * 清除缓存
 * Created by Administrator on 2016/9/13.
 */
public class WipeCacheActivity extends BaseActivity implements View.OnClickListener {
    private static final int M_M = 1024 * 1024;
    private TextView tx_offline_files_size;

    private TextView tx_modle_file_size;
//    private String rootPath = ESystem.getPackagesPath(WipeCacheActivity.this);
    private String wordBookListPath = ESystem.getWordListRecordPath();
//    private File packagesFile = new File(rootPath);

    private float fileSize;
    private float videoFileSize = 0;
    private float modleFileSize = 0;
    private int requestId = -1;

    @Override
    protected int setView() {
        return R.layout.activity_wipe_cache;
    }

    @Override
    protected void findViews() {

        tx_offline_files_size = (TextView) findViewById(R.id.tx_offline_files_size);
        tx_modle_file_size = (TextView) findViewById(R.id.tx_modle_file_size);
    }

    @Override
    protected void initEvent() {

        findViewById(R.id.btn_wipe_package_cache).setOnClickListener(this);
        findViewById(R.id.btn_wipe_modle_cache).setOnClickListener(this);
        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                onBackPressed();

            }
        });
    }

    @Override
    protected void init() {
        setTitleText("清除缓存");

        setFileSize();
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        setResult(RESULT_OK);
        finish();
    }


    private void wipePackageCache() {
        showMiddleProgressBar(getTitleText());
        ThreadTask.getInstance().executorDBThread(new Runnable() {
            @Override
            public void run() {
                DownLoadDB.cleanData();
                DatabaseHelper.clearDatabase();
                FileUtils.delete(ESystem.getPackagesPath(WipeCacheActivity.this));
                FileUtils.delete(wordBookListPath);
                WipeCacheActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tx_offline_files_size.setText("0.0M");
                        hideMiddleProgressBar();
                        //通知首页我的作业刷新状态
                        sendBroadcast(new Intent(EpaperConstant.ACTION_PACKAGE_DELETE));
                    }
                });

            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);

    }



    private void wipeModleCache() {
        final String[] targetPath = {
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.LISTENSPEAKREDO,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.ONLINE_PAPER,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.Interactive_Tutorial,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.NEWVIEDO,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.REGIST};
        showMiddleProgressBar(getTitleText());
        ThreadTask.getInstance().executorDBThread(new Runnable() {
            @Override
            public void run() {
                //删除作业模版缓存
                for (int i = 0; i < targetPath.length; i++) {
                    FileUtils.delete(targetPath[i]);
                }
                WipeCacheActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        hideMiddleProgressBar();
                        tx_modle_file_size.setText("0.0M");
                    }
                });


            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    private void setFileSize() {
        final String[] targetPath = {
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.LISTENSPEAKREDO,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.ONLINE_PAPER,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.Interactive_Tutorial,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.NEWVIEDO,
                ESystem.getProjectFolderPath(WipeCacheActivity.this) + File.separator + ESystem.REGIST};
        ThreadTask.getInstance().executorOtherThread(new Runnable() {
            @Override
            public void run() {
                File packagesFile = new File(ESystem.getPackagesPath(WipeCacheActivity.this));
                fileSize = FileUtil.getFolderSize(packagesFile) / M_M;
                if (videoFileSize > 0) {
                    videoFileSize = videoFileSize / M_M;
                }
                for (int i = 0; i < targetPath.length; i++) {
                    File modleFile = new File(targetPath[i]);
                    modleFileSize = modleFileSize + FileUtil.getFolderSize(modleFile) / M_M;
                }
                WipeCacheActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String size = String.format("%.2f", videoFileSize);
                        tx_offline_files_size.setText(fileSize + "MB");
                        tx_modle_file_size.setText(modleFileSize + "MB");
                    }
                });

            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.btn_wipe_package_cache) {

            requestId = R.id.btn_wipe_package_cache;
            showConfirmWipeDialog(getString(R.string.str_confirm_wipe_package_file));

        } else if (id == R.id.btn_wipe_modle_cache) {

            requestId = R.id.btn_wipe_modle_cache;
            showConfirmWipeDialog(getString(R.string.str_confirm_wipe_modle_file));

        }

    }


    /**
     * 未加入班级提示框
     */
    private void showConfirmWipeDialog(String tips) {
        final CWDialog dialog = new CWDialog(this, false, false);
        dialog.setMessage(tips);
        dialog.setCancelable(true);
        dialog.setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();

            }
        });
        dialog.setPositiveButton(getString(R.string.str_confirm),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (requestId == R.id.btn_wipe_package_cache) {
                            wipePackageCache();

                        } else if (requestId == R.id.btn_wipe_modle_cache) {
                            requestId = R.id.btn_wipe_modle_cache;
                            wipeModleCache();
                        }
                    }
                }, true)
                .show();
    }
}
